package com.plugin.manage.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.plugin.core.model.entity.SysPermission;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 系统权限Mapper接口
 * 
 * @author mark
 * @date 2025-07-28
 */
@Mapper
public interface SysPermissionMapper extends BaseMapper<SysPermission> {

    /**
     * 根据角色ID删除权限
     * 
     * @param roleId 角色ID
     * @return 删除数量
     */
    @Delete("DELETE FROM sys_permission WHERE role_id = #{roleId}")
    int deleteByRoleId(@Param("roleId") Long roleId);

    /**
     * 根据资源ID删除权限
     * 
     * @param resourceId 资源ID
     * @return 删除数量
     */
    @Delete("DELETE FROM sys_permission WHERE resource_id = #{resourceId}")
    int deleteByResourceId(@Param("resourceId") Long resourceId);

    /**
     * 根据角色ID查询权限
     * 
     * @param roleId 角色ID
     * @return 权限列表
     */
    @Select("SELECT * FROM sys_permission WHERE role_id = #{roleId}")
    List<SysPermission> selectByRoleId(@Param("roleId") Long roleId);

    /**
     * 根据资源ID查询权限
     * 
     * @param resourceId 资源ID
     * @return 权限列表
     */
    @Select("SELECT * FROM sys_permission WHERE resource_id = #{resourceId}")
    List<SysPermission> selectByResourceId(@Param("resourceId") Long resourceId);

    /**
     * 检查角色是否有指定资源权限
     * 
     * @param roleId 角色ID
     * @param resourceId 资源ID
     * @return 数量
     */
    @Select("SELECT COUNT(1) FROM sys_permission WHERE role_id = #{roleId} AND resource_id = #{resourceId}")
    int checkRolePermission(@Param("roleId") Long roleId, @Param("resourceId") Long resourceId);

    /**
     * 批量插入权限
     *
     * @param permissions 权限列表
     * @return 插入数量
     */
    @Insert("<script>" +
            "INSERT INTO sys_permission (role_id, resource_id, create_time, update_time) VALUES " +
            "<foreach collection='permissions' item='permission' separator=','>" +
            "(#{permission.roleId}, #{permission.resourceId}, NOW(), NOW())" +
            "</foreach>" +
            "</script>")
    int batchInsert(@Param("permissions") List<SysPermission> permissions);

    /**
     * 根据角色ID列表删除权限
     * 
     * @param roleIds 角色ID列表
     * @return 删除数量
     */
    @Delete("<script>" +
            "DELETE FROM sys_permission WHERE role_id IN " +
            "<foreach collection='roleIds' item='roleId' open='(' separator=',' close=')'>" +
            "#{roleId}" +
            "</foreach>" +
            "</script>")
    int deleteByRoleIds(@Param("roleIds") List<Long> roleIds);

    /**
     * 根据资源ID列表删除权限
     * 
     * @param resourceIds 资源ID列表
     * @return 删除数量
     */
    @Delete("<script>" +
            "DELETE FROM sys_permission WHERE resource_id IN " +
            "<foreach collection='resourceIds' item='resourceId' open='(' separator=',' close=')'>" +
            "#{resourceId}" +
            "</foreach>" +
            "</script>")
    int deleteByResourceIds(@Param("resourceIds") List<Long> resourceIds);

    /**
     * 查询所有权限
     * 
     * @return 权限列表
     */
    @Select("SELECT * FROM sys_permission ORDER BY role_id, resource_id")
    List<SysPermission> selectAllPermissions();

    /**
     * 根据用户ID查询用户权限
     * 
     * @param userId 用户ID
     * @return 权限列表
     */
    @Select("SELECT p.* FROM sys_permission p " +
            "INNER JOIN sys_user_role ur ON p.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId}")
    List<SysPermission> selectPermissionsByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID和资源ID检查权限
     * 
     * @param userId 用户ID
     * @param resourceId 资源ID
     * @return 数量
     */
    @Select("SELECT COUNT(1) FROM sys_permission p " +
            "INNER JOIN sys_user_role ur ON p.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.resource_id = #{resourceId}")
    int checkUserPermission(@Param("userId") Long userId, @Param("resourceId") Long resourceId);

    /**
     * 根据用户ID和权限标识检查权限
     * 
     * @param userId 用户ID
     * @param perms 权限标识
     * @return 数量
     */
    @Select("SELECT COUNT(1) FROM sys_permission p " +
            "INNER JOIN sys_user_role ur ON p.role_id = ur.role_id " +
            "INNER JOIN sys_menu m ON p.resource_id = m.menu_id " +
            "WHERE ur.user_id = #{userId} AND m.perms = #{perms}")
    int checkUserPermissionByPerms(@Param("userId") Long userId, @Param("perms") String perms);

    /**
     * 统计权限数量
     * 
     * @return 数量
     */
    @Select("SELECT COUNT(1) FROM sys_permission")
    int countPermissions();

    /**
     * 根据角色ID统计权限数量
     * 
     * @param roleId 角色ID
     * @return 数量
     */
    @Select("SELECT COUNT(1) FROM sys_permission WHERE role_id = #{roleId}")
    int countPermissionsByRoleId(@Param("roleId") Long roleId);

    /**
     * 分页查询权限列表
     * 
     * @param offset 偏移量
     * @param limit 限制数量
     * @return 权限列表
     */
    @Select("SELECT p.*, r.role_name, m.menu_name " +
            "FROM sys_permission p " +
            "LEFT JOIN sys_role r ON p.role_id = r.role_id " +
            "LEFT JOIN sys_menu m ON p.resource_id = m.menu_id " +
            "ORDER BY p.role_id, p.resource_id " +
            "LIMIT #{offset}, #{limit}")
    List<SysPermission> selectPermissionList(@Param("offset") int offset, @Param("limit") int limit);
}
